const md5 = require('md5')
module.exports = {
	// 数组按value去重
	uniqueArray: (array, name) => {
		let rarray = [];
		let tjson = {};
		array.map(function (a) {
			if (typeof a == 'object') {
				if (!tjson[a[name]]) {
					tjson[a[name]] = true;
					rarray.push(a);
				}
			} else {
				if (!tjson[a]) {
					tjson[a] = true;
					rarray.push(a);
				}
			}
		})
		return rarray;
	},
	// 随机数
	makeCode: n => {
		var num = "";
		for (var i = 0; i < n; i++) {
			num += Math.floor(Math.random() * 10)
		}
		return num;
	},
	getWXSing: (params, shopKey) => {
		let str = []
		for (const key in params) {
			str.push(key + '=' + params[key])
		}

		str.sort(function (a, b) {
			return a.localeCompare(b)
		})
		str.push('key=' + shopKey)
		str = str.join('&')
		str = md5(str)
		return str
	},
	// 处理sql返回数据
	handelData:(data)=>{
		return JSON.parse(JSON.stringify(data))
	},

	// 数组排序
	sortArr:(arr,num)=>{
		const sortArrOld =(x,y,num)=>{
			return y[num]-x[num];
		}
		return arr.sort(sortArrOld)
	}
}